package de.ars.client.items;

import java.util.Vector;

import de.ars.client.datahandling.IPersonListener;
import de.ars.client.datahandling.Person;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.items.animators.AddPersonItemAnimator;
import de.ars.client.items.elements.AbstractItem;
import de.ars.client.items.elements.ChooseItem;
import de.ars.client.items.elements.ListItem;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;

public class AddPersonItem extends AbstractSlideItem
{	
	public AddPersonItem(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
	{
		super(_width,_height,_parent,_stringPainter);
		
    	this.animator = new AddPersonItemAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getAddPersonClosePos());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseTextPos());
        this.animator.setLayout(this.layout);
        
        this.entryItems = new Vector();
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("label.person.firstname") + ": ", "person.firstname"));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("label.person.lastname")+ ": ", "person.lastname"));
        ListItem tempItem = new ListItem(this.stringPainter, PropertyReader.getProperty("label.person.tel1")+ ": ", "person.tel1");
        tempItem.setType(ListItem.LISTITEM_TYPE_DIGIT);
        this.entryItems.addElement(tempItem);
        Vector genderOptions = new Vector();
        genderOptions.addElement("gender.male");
        genderOptions.addElement("gender.female");
        this.entryItems.addElement(new ChooseItem(this.stringPainter, PropertyReader.getProperty("label.person.gender")+ ": ", "person.gender", genderOptions)); 
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("label.person.city")+ ": ", "person.city"));        
	}
	
	private void savePerson()
	{
		Person pTemp = new Person();
		for (int i = 0;i < this.entryItems.size();i++)
			pTemp.putInformation(((AbstractItem)this.entryItems.elementAt(i)).getKey(), ((AbstractItem)this.entryItems.elementAt(i)).getValue());
		
		this.parent.newPersonAction(IPersonListener.ACTION_NEWADDEDPERSON, pTemp);
		/*for(int i = 0; i < this.defaultEntryItems.size(); i++)
			((ListItem)this.defaultEntryItems.elementAt(i)).resetTextElements();*/
	}
	
	public boolean keyPressed(int _key)
	{
		if (isOpen())
		{
			if (!super.keyPressed(_key))
			{
				AbstractItem tempItem = ((AbstractItem)this.entryItems.elementAt(this.selectedItemIndex));
				switch(_key)
				{
		    		case -6:
		    			this.animationType = ANIMATION_CLOSE;
		    			this.animator.closeItem();
		    			this.state = STATE_CLOSE_ANIMATION;
		    			return true;
		    		case -7:
		    			//speichern und fenster schliessen
		    			this.savePerson();
		    			this.animationType = ANIMATION_CLOSE;
		    			this.animator.closeItem();
		    			this.state = STATE_CLOSE_ANIMATION;
		    			return true;
		    		default:
		        		tempItem.keyPressed(_key);
		        		return true;
		    	}
			}
		}
		return false;
	}
}